import { FieldMetadataType, RelationType } from 'twenty-shared/types';

import { type FlatFieldMetadata } from 'src/engine/metadata-modules/flat-field-metadata/types/flat-field-metadata.type';
import { type AllStandardObjectFieldName } from 'src/engine/workspace-manager/twenty-standard-application/types/all-standard-object-field-name.type';
import { createStandardFieldFlatMetadata } from 'src/engine/workspace-manager/twenty-standard-application/utils/create-standard-field-flat-metadata.util';
import { createStandardRelationFieldFlatMetadata } from 'src/engine/workspace-manager/twenty-standard-application/utils/create-standard-relation-field-flat-metadata.util';
import { type StandardFieldMetadataIdByObjectAndFieldName } from 'src/engine/workspace-manager/twenty-standard-application/utils/get-standard-field-metadata-id-by-object-and-field-name.util';

export const buildCalendarEventStandardFlatFieldMetadatas = ({
  createdAt,
  workspaceId,
  standardFieldMetadataIdByObjectAndFieldName,
}: {
  createdAt: Date;
  workspaceId: string;
  standardFieldMetadataIdByObjectAndFieldName: StandardFieldMetadataIdByObjectAndFieldName;
}): Record<AllStandardObjectFieldName<'calendarEvent'>, FlatFieldMetadata> => ({
  id: createStandardFieldFlatMetadata({
    objectName: 'calendarEvent',
    workspaceId,
    options: {
      fieldName: 'id',
      type: FieldMetadataType.UUID,
      label: 'Id',
      description: 'Id',
      icon: 'Icon123',
      isSystem: true,
      isNullable: false,
      isUIReadOnly: true,
      defaultValue: 'uuid',
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  createdAt: createStandardFieldFlatMetadata({
    objectName: 'calendarEvent',
    workspaceId,
    options: {
      fieldName: 'createdAt',
      type: FieldMetadataType.DATE_TIME,
      label: 'Creation date',
      description: 'Creation date',
      icon: 'IconCalendar',
      isNullable: false,
      isUIReadOnly: true,
      defaultValue: 'now',
      settings: { displayFormat: 'RELATIVE' },
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  updatedAt: createStandardFieldFlatMetadata({
    objectName: 'calendarEvent',
    workspaceId,
    options: {
      fieldName: 'updatedAt',
      type: FieldMetadataType.DATE_TIME,
      label: 'Last update',
      description: 'Last time the record was changed',
      icon: 'IconCalendarClock',
      isNullable: false,
      isUIReadOnly: true,
      defaultValue: 'now',
      settings: { displayFormat: 'RELATIVE' },
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  deletedAt: createStandardFieldFlatMetadata({
    objectName: 'calendarEvent',
    workspaceId,
    options: {
      fieldName: 'deletedAt',
      type: FieldMetadataType.DATE_TIME,
      label: 'Deleted at',
      description: 'Date when the record was deleted',
      icon: 'IconCalendarMinus',
      isNullable: true,
      isUIReadOnly: true,
      settings: { displayFormat: 'RELATIVE' },
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  title: createStandardFieldFlatMetadata({
    objectName: 'calendarEvent',
    workspaceId,
    options: {
      fieldName: 'title',
      type: FieldMetadataType.TEXT,
      label: 'Title',
      description: 'Title',
      icon: 'IconH1',
      isNullable: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  isCanceled: createStandardFieldFlatMetadata({
    objectName: 'calendarEvent',
    workspaceId,
    options: {
      fieldName: 'isCanceled',
      type: FieldMetadataType.BOOLEAN,
      label: 'Is canceled',
      description: 'Is canceled',
      icon: 'IconCalendarCancel',
      isNullable: false,
      defaultValue: false,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  isFullDay: createStandardFieldFlatMetadata({
    objectName: 'calendarEvent',
    workspaceId,
    options: {
      fieldName: 'isFullDay',
      type: FieldMetadataType.BOOLEAN,
      label: 'Is Full Day',
      description: 'Is Full Day',
      icon: 'IconHours24',
      isNullable: false,
      defaultValue: false,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  startsAt: createStandardFieldFlatMetadata({
    objectName: 'calendarEvent',
    workspaceId,
    options: {
      fieldName: 'startsAt',
      type: FieldMetadataType.DATE_TIME,
      label: 'Start Date',
      description: 'Start Date',
      icon: 'IconCalendarClock',
      isNullable: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  endsAt: createStandardFieldFlatMetadata({
    objectName: 'calendarEvent',
    workspaceId,
    options: {
      fieldName: 'endsAt',
      type: FieldMetadataType.DATE_TIME,
      label: 'End Date',
      description: 'End Date',
      icon: 'IconCalendarClock',
      isNullable: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  externalCreatedAt: createStandardFieldFlatMetadata({
    objectName: 'calendarEvent',
    workspaceId,
    options: {
      fieldName: 'externalCreatedAt',
      type: FieldMetadataType.DATE_TIME,
      label: 'Creation DateTime',
      description: 'Creation DateTime',
      icon: 'IconCalendarPlus',
      isNullable: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  externalUpdatedAt: createStandardFieldFlatMetadata({
    objectName: 'calendarEvent',
    workspaceId,
    options: {
      fieldName: 'externalUpdatedAt',
      type: FieldMetadataType.DATE_TIME,
      label: 'Update DateTime',
      description: 'Update DateTime',
      icon: 'IconCalendarCog',
      isNullable: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  description: createStandardFieldFlatMetadata({
    objectName: 'calendarEvent',
    workspaceId,
    options: {
      fieldName: 'description',
      type: FieldMetadataType.TEXT,
      label: 'Description',
      description: 'Description',
      icon: 'IconFileDescription',
      isNullable: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  location: createStandardFieldFlatMetadata({
    objectName: 'calendarEvent',
    workspaceId,
    options: {
      fieldName: 'location',
      type: FieldMetadataType.TEXT,
      label: 'Location',
      description: 'Location',
      icon: 'IconMapPin',
      isNullable: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  iCalUID: createStandardFieldFlatMetadata({
    objectName: 'calendarEvent',
    workspaceId,
    options: {
      fieldName: 'iCalUID',
      type: FieldMetadataType.TEXT,
      label: 'iCal UID',
      description: 'iCal UID',
      icon: 'IconKey',
      isNullable: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  conferenceSolution: createStandardFieldFlatMetadata({
    objectName: 'calendarEvent',
    workspaceId,
    options: {
      fieldName: 'conferenceSolution',
      type: FieldMetadataType.TEXT,
      label: 'Conference Solution',
      description: 'Conference Solution',
      icon: 'IconScreenShare',
      isNullable: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  conferenceLink: createStandardFieldFlatMetadata({
    objectName: 'calendarEvent',
    workspaceId,
    options: {
      fieldName: 'conferenceLink',
      type: FieldMetadataType.LINKS,
      label: 'Meet Link',
      description: 'Meet Link',
      icon: 'IconLink',
      isNullable: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  calendarChannelEventAssociations: createStandardRelationFieldFlatMetadata({
    objectName: 'calendarEvent',
    workspaceId,
    options: {
      fieldName: 'calendarChannelEventAssociations',
      label: 'Calendar Channel Event Associations',
      description: 'Calendar Channel Event Associations',
      icon: 'IconCalendar',
      isNullable: false,
      createdAt,
      targetObjectName: 'calendarChannelEventAssociation',
      targetFieldName: 'calendarEvent',
      settings: {
        relationType: RelationType.ONE_TO_MANY,
      },
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  calendarEventParticipants: createStandardRelationFieldFlatMetadata({
    objectName: 'calendarEvent',
    workspaceId,
    options: {
      fieldName: 'calendarEventParticipants',
      label: 'Event Participants',
      description: 'Event Participants',
      icon: 'IconUserCircle',
      isNullable: false,
      createdAt,
      targetObjectName: 'calendarEventParticipant',
      targetFieldName: 'calendarEvent',
      settings: {
        relationType: RelationType.ONE_TO_MANY,
      },
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
});
